package com.ww.learn.coroutine.flow

import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking

fun simple5(): Flow<Int> = flow {
    println("Flow started")
    for (i in 1..3) {
        delay(100)
        emit(i)
    }
}

/**
 * Calling simple function...
 * Calling collect...
 * Flow started
 * 1
 * 2
 * 3
 * Calling collect again...
 * Flow started
 * 1
 * 2
 * 3
 *
 * 流在每次收集的时候启动，这就是为什么当我们再次调用collect时我们会看到“Flow started”
 */
fun main() = runBlocking {
    println("Calling simple function...")
    val flow = simple5()
    println("Calling collect...")
    flow.collect { value -> println(value) }
    println("Calling collect again...")
    flow.collect { value -> println(value) }
}